<template>
  <view class="change-password-container">
    <!-- 页面头部 -->
    <view class="page-header">
      <view class="header-content">
        <text class="title">修改密码</text>
        <text class="subtitle">请设置一个新的安全密码</text>
      </view>
    </view>

    <!-- 密码表单 -->
    <view class="form-section">
      <view class="form-card">
        <view class="form-item">
          <text class="form-label">当前密码</text>
          <view class="input-wrapper">
            <input
              v-model="form.oldPassword"
              placeholder="请输入当前密码"
              type="password"
              class="form-input"
              :disabled="loading"
              @input="validateForm"
            />
            <view class="input-icon" @click="togglePasswordVisibility('old')">
              <text class="icon">{{ showOldPassword ? '👁️' : '👁️‍🗨️' }}</text>
            </view>
          </view>
        </view>

        <view class="form-item">
          <text class="form-label">新密码</text>
          <view class="input-wrapper">
            <input
              v-model="form.newPassword"
              placeholder="请输入新密码（6-20位）"
              type="password"
              class="form-input"
              :disabled="loading"
              @input="validateForm"
            />
            <view class="input-icon" @click="togglePasswordVisibility('new')">
              <text class="icon">{{ showNewPassword ? '👁️' : '👁️‍🗨️' }}</text>
            </view>
          </view>
          <view class="password-strength" v-if="form.newPassword">
            <text class="strength-label">密码强度：</text>
            <view class="strength-bar">
              <view class="strength-fill" :class="passwordStrength.class" :style="{ width: passwordStrength.width }"></view>
            </view>
            <text class="strength-text">{{ passwordStrength.text }}</text>
          </view>
        </view>

        <view class="form-item">
          <text class="form-label">确认新密码</text>
          <view class="input-wrapper">
            <input
              v-model="form.confirmPassword"
              placeholder="请再次输入新密码"
              type="password"
              class="form-input"
              :disabled="loading"
              @input="validateForm"
            />
            <view class="input-icon" @click="togglePasswordVisibility('confirm')">
              <text class="icon">{{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}</text>
            </view>
          </view>
          <view class="password-match" v-if="form.confirmPassword">
            <text class="match-text" :class="{ matched: passwordsMatch }">
              {{ passwordsMatch ? '✓ 密码匹配' : '✗ 密码不匹配' }}
            </text>
          </view>
        </view>

        <view class="password-tips">
          <text class="tips-title">密码安全提示：</text>
          <view class="tips-list">
            <text class="tip-item">• 密码长度6-20位</text>
            <text class="tip-item">• 建议包含字母、数字和特殊字符</text>
            <text class="tip-item">• 避免使用生日、手机号等个人信息</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="action-section">
      <button
        @click="handleSubmit"
        :disabled="loading || !canSubmit"
        class="btn btn-primary"
        :class="{ 'btn-disabled': loading || !canSubmit }"
      >
        <text class="btn-text">{{ loading ? '修改中...' : '确认修改' }}</text>
      </button>

      <button
        @click="goBack"
        :disabled="loading"
        class="btn btn-secondary"
      >
        <text class="btn-text">取消</text>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useUserStore } from '@/src/store/user'

// 用户存储
const userStore = useUserStore()

// 表单数据
const form = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const loading = ref(false)
const showOldPassword = ref(false)
const showNewPassword = ref(false)
const showConfirmPassword = ref(false)

// 密码强度计算
const passwordStrength = computed(() => {
  const password = form.value.newPassword
  if (!password) return { text: '', class: '', width: '0%' }

  let score = 0
  if (password.length >= 6) score++
  if (password.length >= 8) score++
  if (password.length >= 12) score++
  if (/[a-z]/.test(password)) score++
  if (/[A-Z]/.test(password)) score++
  if (/[0-9]/.test(password)) score++
  if (/[^a-zA-Z0-9]/.test(password)) score++

  if (score <= 2) return { text: '弱', class: 'weak', width: '33%' }
  if (score <= 4) return { text: '中', class: 'medium', width: '66%' }
  return { text: '强', class: 'strong', width: '100%' }
})

// 密码匹配检查
const passwordsMatch = computed(() => {
  return form.value.newPassword && form.value.confirmPassword &&
         form.value.newPassword === form.value.confirmPassword
})

// 表单验证
const canSubmit = computed(() => {
  return form.value.oldPassword.trim() !== '' &&
         form.value.newPassword.trim() !== '' &&
         form.value.confirmPassword.trim() !== '' &&
         form.value.newPassword.length >= 6 &&
         form.value.newPassword.length <= 20 &&
         passwordsMatch.value
})

// 切换密码可见性
const togglePasswordVisibility = (field: 'old' | 'new' | 'confirm') => {
  switch (field) {
    case 'old':
      showOldPassword.value = !showOldPassword.value
      break
    case 'new':
      showNewPassword.value = !showNewPassword.value
      break
    case 'confirm':
      showConfirmPassword.value = !showConfirmPassword.value
      break
  }
}

// 表单验证
const validateForm = () => {
  // 实时验证逻辑
}

// 提交处理
const handleSubmit = async () => {
  if (loading.value || !canSubmit.value) return

  // 最终验证
  if (form.value.newPassword.length < 6 || form.value.newPassword.length > 20) {
    uni.showToast({
      title: '新密码长度应为6-20位',
      icon: 'none'
    })
    return
  }

  if (form.value.newPassword === form.value.oldPassword) {
    uni.showToast({
      title: '新密码不能与旧密码相同',
      icon: 'none'
    })
    return
  }

  try {
    loading.value = true

    // 调用修改密码API
    const success = await userStore.changePassword(form.value.oldPassword, form.value.newPassword)

    if (success) {
      uni.showToast({
        title: '密码修改成功',
        icon: 'success'
      })

      // 延迟返回
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }

  } catch (error: any) {
    uni.showToast({
      title: error.message || '密码修改失败',
      icon: 'none'
    })
    console.error('修改密码失败:', error)
  } finally {
    loading.value = false
  }
}

// 返回
const goBack = () => {
  uni.navigateBack()
}
</script>

<style lang="scss" scoped>
.change-password-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f6ffed 0%, #e6f7ff 100%);
  padding-bottom: 40rpx;
}

// 页面头部
.page-header {
  background: linear-gradient(135deg, #52c41a, #73d13d);
  padding: 48rpx 32rpx 32rpx;
  color: #ffffff;

  .header-content {
    text-align: center;
  }

  .title {
    font-size: 36rpx;
    font-weight: bold;
    margin-bottom: 8rpx;
  }

  .subtitle {
    font-size: 26rpx;
    opacity: 0.9;
  }
}

// 表单区域
.form-section {
  padding: 0 32rpx;
  margin-top: -20rpx;

  .form-card {
    background: #ffffff;
    border-radius: 24rpx;
    padding: 32rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);

    .form-item {
      margin-bottom: 32rpx;

      &:last-child {
        margin-bottom: 0;
      }

      .form-label {
        display: block;
        font-size: 28rpx;
        color: #333;
        font-weight: 500;
        margin-bottom: 12rpx;
      }

      .input-wrapper {
        position: relative;
        display: flex;
        align-items: center;

        .form-input {
          flex: 1;
          height: 88rpx;
          padding: 0 80rpx 0 24rpx;
          border: 2rpx solid #e8e8e8;
          border-radius: 12rpx;
          font-size: 30rpx;
          background: #fafafa;
          transition: all 0.3s ease;

          &:focus {
            border-color: #52c41a;
            background: #ffffff;
            box-shadow: 0 0 0 4rpx rgba(82, 196, 26, 0.1);
          }
        }

        .input-icon {
          position: absolute;
          right: 24rpx;
          top: 50%;
          transform: translateY(-50%);
          font-size: 32rpx;
          color: #999;
          cursor: pointer;
          padding: 8rpx;

          &:active {
            opacity: 0.7;
          }
        }
      }

      .password-strength {
        display: flex;
        align-items: center;
        margin-top: 12rpx;
        gap: 16rpx;

        .strength-label {
          font-size: 24rpx;
          color: #666;
        }

        .strength-bar {
          flex: 1;
          height: 8rpx;
          background: #f0f0f0;
          border-radius: 4rpx;
          overflow: hidden;

          .strength-fill {
            height: 100%;
            transition: all 0.3s ease;

            &.weak {
              background: #ff4d4f;
            }

            &.medium {
              background: #faad14;
            }

            &.strong {
              background: #52c41a;
            }
          }
        }

        .strength-text {
          font-size: 24rpx;
          font-weight: 500;

          &.weak {
            color: #ff4d4f;
          }

          &.medium {
            color: #faad14;
          }

          &.strong {
            color: #52c41a;
          }
        }
      }

      .password-match {
        margin-top: 8rpx;

        .match-text {
          font-size: 24rpx;

          &.matched {
            color: #52c41a;
          }

          &:not(.matched) {
            color: #ff4d4f;
          }
        }
      }
    }

    .password-tips {
      margin-top: 32rpx;
      padding: 24rpx;
      background: #f8f9fa;
      border-radius: 12rpx;

      .tips-title {
        display: block;
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        margin-bottom: 12rpx;
      }

      .tips-list {
        display: flex;
        flex-direction: column;
        gap: 8rpx;

        .tip-item {
          font-size: 24rpx;
          color: #666;
          line-height: 1.5;
        }
      }
    }
  }
}

// 操作按钮
.action-section {
  padding: 0 32rpx;
  margin-top: 48rpx;

  .btn {
    width: 100%;
    height: 88rpx;
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24rpx;
    transition: all 0.3s ease;

    &:active {
      transform: scale(0.98);
    }

    &.btn-primary {
      background: linear-gradient(135deg, #52c41a, #73d13d);
      box-shadow: 0 4rpx 12rpx rgba(82, 196, 26, 0.3);

      &.btn-disabled {
        background: #ccc;
        box-shadow: none;
        transform: none;
      }

      .btn-text {
        font-size: 32rpx;
        color: #ffffff;
        font-weight: 500;
      }
    }

    &.btn-secondary {
      background: #ffffff;
      border: 2rpx solid #e8e8e8;

      .btn-text {
        font-size: 32rpx;
        color: #666;
        font-weight: 500;
      }
    }
  }
}
</style>